package com.example.myretrofit2.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.example.myretrofit2.R;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.view.MyWebView;


public class HomeFragment extends Fragment {
SwipeRefreshLayout swipeRefreshLayout;
MyWebView webView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_home, container, false);
        swipeRefreshLayout = view.findViewById(R.id.swlayout);
        webView = view.findViewById(R.id.mywebview);
        initWebView(view);
        initswipeRefreshLaout();
        Button button = view.findViewById(R.id.testjs);
        button.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
               webView.evaluateJavascript("javascript:jsfun()", new ValueCallback<String>() {
                   @Override
                   public void onReceiveValue(String s) {
                       System.out.println(s);
                   }
               });
            }
        });
        return view;
    }
    @SuppressLint("JavascriptInterface")
    public void initWebView(View view){
        webView.addJavascriptInterface(this,"myandroid");
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        webView.setiMyWebView(new MyWebView.IMyWebView() {
            @Override
            public void onTop() {
                swipeRefreshLayout.setEnabled(true);
            }

            @Override
            public void onNotTop() {
                swipeRefreshLayout.setEnabled(false);
            }
        });
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if(keyEvent.getAction()==keyEvent.ACTION_DOWN){
                    if(i==keyEvent.KEYCODE_BACK&&webView.canGoBack()){
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            }
        });
        webView.setWebViewClient(new WebViewClient(){
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
               swipeRefreshLayout.setRefreshing(true);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        webView.loadUrl(MyApplication.webUrl);
    }
    public void initswipeRefreshLaout(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });
    }
}